package com.office.web;

import com.office.bean.Emp;
import com.office.bean.ImageMessage;
import com.office.service.UserMange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class UserMangerControl {
    @Autowired
    UserMange userMange;

    @RequestMapping("/User_updatepassword")
    public void update(String oldpass, String newpass, HttpSession session, HttpServletResponse resp) throws IOException {
         resp.setContentType("text/html;charset=utf-8");
        Emp oldemp = (Emp)session.getAttribute("loginuser");
         if (oldemp.getPass().equals(oldpass)) {//原密码输入正确
             int updateuser = userMange.updateuser((int) oldemp.getId(), newpass);//修改数据库用户的密码
             if (updateuser>0){
                 session.invalidate();
                 resp.getWriter().write("<script>alert('密码修改成功');parent.location.href='/';</script>");
             }else {
                 resp.getWriter().write("<script>alert('密码修改失败');location.href='/html_password';</script>");
             }
         }else {
             resp.getWriter().write("<script>alert('原密码输入错误');location.href='/html_password';</script>");

         }
    }
    @RequestMapping("/User_updatephoto")
    public void updatephoto(HttpSession session, MultipartFile file,HttpServletResponse resp) {
        resp.setContentType("text/html;charset=utf-8");
        String originalFilename = file.getOriginalFilename();//获取上传到文件的名字
        if (originalFilename.isEmpty()) {
            try {
                resp.getWriter().write("<script>alert('请选择图片');location.href='/html_photo';</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("更新的图片的名字为" + originalFilename);
            String suffix = originalFilename.split("\\.")[1];//取.后面的值
            String uuid = UUID.randomUUID().toString();
            //拼接新名字
            String newName = uuid + "." + suffix;

            //获取老文件的路径
            Emp oldemp = (Emp) session.getAttribute("loginuser");
            String oldempPhoto = oldemp.getPhoto();//获取路径
            try {
                //取class文件里的图片（目的：发送给前端，指明路径）
                String path = session.getServletContext().getRealPath("/media/images") + "/" + newName;

                //文件上传
                file.transferTo(new File(path));
                System.out.println("文件上传成功");
                //删除老文件
                String oldpath = session.getServletContext().getRealPath("/media/images") + "/" + oldempPhoto;

                File oldfile = new File(oldpath);
                if (oldfile.exists()) {
                    oldfile.delete();
                }
                oldemp.setPhoto(newName);
                int updatphoto = userMange.updatphoto(oldemp);
                System.out.println("修改后插入数据库的为"+updatphoto);
                resp.getWriter().write("<script>alert('头像修改成功');parent.location.href='/html_index';</script>");


            } catch (IOException e) {
                try {
                    resp.getWriter().write("<script>alert('头像修改失败');location.href='/html_photo';</script>");
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
//        ImageMessage imageMessage = new ImageMessage(1000, newName);
        }
    }
}
